package com.lin.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class RandomGenerate {

    /**
     * 随机生成数值字符串类型
     * @param length
     * @return
     */
    public static String generateRandomNumericString(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int digit = random.nextInt(10); // 生成0到9的随机数字
            sb.append(digit);
        }

        return sb.toString();
    }

    /**
     * 随机生成时间
     * @return
     */
    public static Date generateRandomDate() {
        // 设置起始日期和结束日期，例如近12个月内的数据
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        Date startDate = calendar.getTime();
        Date endDate = new Date(); // 当前日期

        long startTimeMillis = startDate.getTime();
        long endTimeMillis = endDate.getTime();

        // 随机生成一个日期，范围在起始日期和结束日期之间
        long randomTimeMillis = startTimeMillis + (long) (Math.random() * (endTimeMillis - startTimeMillis));
        return new Date(randomTimeMillis);
    }

    /**
     * 随机生成字符串
     * @param length
     * @return
     */
    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int index = new Random().nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return sb.toString();
    }

    public static double generateLat(double length){
        // 生成在 -90 到 90 之间的随机双精度浮点数
        Random random = new Random();
        double min = -90.0;
        double max = 90.0;
        return Math.round(min + (max - min) * random.nextDouble() * length) / length;
    }

    public static double generateLng(){
        // 生成在 -180 到 180 之间的随机双精度浮点数
        Random random = new Random();
        double min = -180.0;
        double max = 180.0;
        return Math.round(min + (max - min) * random.nextDouble() * 10000000.0) / 10000000.0;
    }
}
